package com.zhxy.manager.controller;

import com.alibaba.excel.EasyExcel;
import com.zhxy.manager.entity.Association;
import com.zhxy.manager.entity.Course;
import com.zhxy.manager.entity.Dormitory;
import com.zhxy.manager.service.intf.DormitoryService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 宿舍人员管理
 *
 * @author ZM
 */
@RestController
@RequestMapping("/api/dormitory/")
public class DormitoryController {
    @Autowired
    private DormitoryService service;

    //新增
    @PostMapping("save.do")
    public R save(Dormitory dormitory) {
        return service.save(dormitory);
    }

    //查询
    @GetMapping("page.do")
    public PageBean page(int page,int limit) {
        return service.queryPage(page,limit);
    }
    //删除
    @PostMapping("del.do")
    public R del(int id) {
        return service.del(id);
    }
    //修改
    @PostMapping("update.do")
    public R update(Dormitory dormitory) {
        return service.update(dormitory);
    }
    //查询  根据条件查询
    @GetMapping("pagewhere.do")
    public PageBean page(String name,int page,int limit) {
        return service.queryPageWhere(name,page,limit);
    }

    //数据导出
    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Dormitory> list=service.queryAll();
        //2.设置响应消息头
        response.setHeader("Content-disposition", "attachment;filename=" +System.currentTimeMillis() + ".xlsx");
        //3.写出内容
        EasyExcel.write(response.getOutputStream(),Dormitory.class).
                sheet("自动导出").doWrite(list);
    }

    //数据导入，Excel上传-解析-批量新增
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.saveBatch(file);
    }

    //批量删除  json格式请求
    @PostMapping("delbatch.do")
    public R del(@RequestBody int[] ids) {
        return service.delBatch(ids);
    }

}
